<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <link href="../../static/layui/css/layui.css" rel="stylesheet">
    <script src="../../static/layui/layui.js"></script>
    <style>
        .container {
            padding: 25px;
        }
    </style>
</head>
<body>
<!--行工具栏模板-->
<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delete">删除</a>
        <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="reset">重置密码</a>
    </div>
</script>
<!--头部工具栏模板-->
<script type="text/html" id="userBar">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-add-circle-fine"></i>添加
        </a>
        <a class="layui-btn layui-btn-sm layui-bg-red" lay-event="batchDelete">
            <i class="layui-icon layui-icon-delete"></i> 批量删除
        </a>
    </div>
</script>

<div class="container">
    <div class="search">
        <form class="layui-form userForm layui-row layui-col-space16">
            <div class="layui-col-md4">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="memberName" value="" placeholder="请输入会员名" class="layui-input"
                           lay-affix="clear">
                </div>
            </div>
            <div class="layui-col-md4">
                <button class="layui-btn" lay-submit lay-filter="demo-table-search">搜索</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </form>
    </div>

    <table class="layui-hide" id="ID-table-demo-data" lay-filter="user-filter"></table>
</div>
<script>
    layui.use(function () {
        // 获取表格对象
        let table = layui.table,
        form = layui.form,
            $ = layui.$,
        layer = layui.layer;
        // 已知数据渲染
        let inst = table.render({
            elem: '#ID-table-demo-data',
            id:'member',
            url: "/ssm/member/page",
            toolbar: '#userBar',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'memberName', title: '会员名'},
                {field: 'idCard', title: '身份证号', minWidth: 160},
                {field: 'sex', title: '性别'},
                {field: 'tel', title: '联系方式'},
                {field: 'level', title: '会员等级'},
                {field: 'createTime', title: '创建时间'},
                {fixed: 'right', title: '操作', width: 200, toolbar: '#barDemo'}
            ]],
            skin: 'line',
            even: true,
            page: true,
            limits: [5, 10, 15, 20, 30, 50],
            limit: 5
        });


        form.on('submit(demo-table-search)',function (obj){
            let  searchData = obj.field;
            //  重载表格数据
            table.reload('member', {
                where: { // 传递数据异步请求时携带的字段
                    memberName:searchData.memberName
                }
            });

            return false;  //关闭默认提交
        })


        table.on('toolbar(user-filter)', function (obj) {
            let id = obj.config.id;
            let checkStatus = table.checkStatus(id);
            let infos = checkStatus.data  // [{},{}]
            let ids = infos.map(info => info.id);
            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '新增',
                        type: 2,
                        area: ['30%', '70%'],
                        end:function (){
                            table.reload('member');
                        },
                        content: 'add.html'
                    });
                    //  发送异步请求 到后台完成用户信息添加
                    break;
                case 'batchDelete':
                    //  发送异步请求
                    let ids = infos.map(info=>info.id);  // []
                    if (ids.length==0){
                        layer.msg("请至少选择一条数据")
                        return;
                    }
                    $.ajax({
                        url:'/ssm/member/delete',
                        data: {ids},
                        traditional:true,  // 不要对数据做深度格式化  ?ids=18&ids=10
                        type:"get",
                        dataType:'json',
                        success:function (res){
                            if (res.code==0){  //  删除成功
                                layer.msg(res.message,{icon:1},function (){
                                    table.reload("member",{
                                        page:{
                                            curr:1,
                                            limit:5
                                        }
                                    })  //  表格数据的重载
                                })
                            }else{
                                layer.msg(res.message,{icon:2})
                            }
                        }
                    })
                    break;
            }
            ;
        });

        // 触发单元格工具事件
        table.on('tool(user-filter)', function (obj) { // 双击 toolDouble
            let data = obj.data; // 获得当前行数据
            if (obj.event === 'edit') {

                // 发送异步请求 获取会员信息  数据回显
                $.get("/ssm/member/getById",{id:data.id},function (res){
                    if (res.code==0){
                        sessionStorage.setItem("member",JSON.stringify(res.data))
                        layer.open({
                            title: '编辑',
                            type: 2,
                            area: ['30%', '70%'],
                            end:function (){
                                table.reload('member');
                            },
                            content: 'edit.html'
                        });
                    }
                })
            } else if (obj.event === 'delete') {
                let  id = data.id;
                //  发送异步请求
                $.ajax({
                    url:'/ssm/member/delete',
                    data: {ids:id} ,
                    type:"get",
                    dataType:'json',
                    success:function (res){
                        if (res.code==0){  //  删除成功
                            layer.msg(res.message,{icon:1},function (){
                                table.reload("member",{
                                    page:{
                                        curr:1,
                                        limit:5
                                    }
                                })
                            })
                        }else{
                            layer.msg(res.message,{icon:2})
                        }
                    }
                })
            } else if (obj.event === 'reset') {
                //  发送异步请求
                let  id = data.id;
                //  发送异步请求
                $.ajax({
                    url:'/ssm/member/reset',
                    data: {id:id} ,
                    type:"get",
                    dataType:'json',
                    success:function (res){
                        if (res.code==0){
                            layer.msg(res.message,{icon:1})
                        }else{
                            layer.msg(res.message,{icon:2})
                        }
                    }
                })
            }

        });


    });
</script>

</body>
</html>
